/***********************************************************************************************************************
* Copyright (C) All rights reserved.
***********************************************************************************************************************/

/***********************************************************************************************************************
* @file    adc_user.c
* @brief   This file implements device driver for ADC module.
* @version 1.0.0 
* @date    2019/12/24
***********************************************************************************************************************/

/***********************************************************************************************************************
Includes
***********************************************************************************************************************/
#include "BAT32G137.h"
#include "adc.h"
/* Start user code for include. Do not edit comment generated here */
/* End user code. Do not edit comment generated here */
#include "userdefine.h"

/***********************************************************************************************************************
Pragma directive
***********************************************************************************************************************/
//void IRQ21_Handler(void) __attribute__((alias("adc_interrupt")));

/* Start user code for pragma. Do not edit comment generated here */
/* End user code. Do not edit comment generated here */

/***********************************************************************************************************************
Global variables and functions
***********************************************************************************************************************/
/* Start user code for global. Do not edit comment generated here */
volatile uint16_t * gp_adc_buf;           /* adc 12 bit result buffer address */
volatile uint32_t   g_AdcIntTaken;           /* adc interrupt flag */
/* End user code. Do not edit comment generated here */

/***********************************************************************************************************************
* Function Name: adc_interrupt
* @brief  ADC interrupt service routine
* @param  None
* @return None
***********************************************************************************************************************/
void adc_interrupt(void)
{
    INTC_ClearPendingIRQ(ADC_IRQn);     /* clear INTAD interrupt flag */
    /* Start user code. Do not edit comment generated here */
    #if 0
    if (0x2000==((uint32_t)gp_adc_buf >> 16)){          // Pointer judgememt (save to RAM)
        *gp_adc_buf++ = ADC->ADCR;
    }
    #endif
    g_AdcIntTaken++;
    /* End user code. Do not edit comment generated here */
}

/***********************************************************************************************************************
* Function Name: adc_get_temperature
* @brief  ADC Get temperature
* @param  addat - the adc conversion value of ADC_TEMPERSENSOR0 channel
* @param  adRefVolt - the adc reference voltage value of ADC module
* @return temperature
***********************************************************************************************************************/
int16_t adc_get_temperature(int16_t addat, double adRefVolt)
{
    int16_t temp;   /* temperature value */
    int16_t addat_adj;
    
    addat_adj = (int16_t)(addat*adRefVolt)/3.0;
    temp = (int16_t)(TSN->TSN25 - addat_adj) * 60 / (TSN->TSN25 - TSN->TSN85) + 25; /* 12bit dat */

    return (temp);
}

/* Start user code for adding. Do not edit comment generated here */
/* End user code. Do not edit comment generated here */
